﻿using System;
using System.Threading.Tasks;
using NetMQ.Sockets;
using NetMQ;
using Bit.Art.Distributed;
using Bit.Art.Core.Extensions;
using Bit.Art.Core.Models;

namespace bit.art.websocket
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("====== WEB SOCKET ======");
            var topic = args[0];
            if (string.Compare(topic, "ALL", true) == 0)
                topic = "";

            StartEngine(topic);
            Console.ReadLine();
        }

        public static Task StartEngine(string topic)
        {
            return Task.Run(() =>
            {
                using (var subscriber = new SubscriberSocket())
                {
                    subscriber.Options.ReceiveHighWatermark = SiteConfiguration.PublisherSendHighWatermark;
                    subscriber.Connect(SiteConfiguration.PublishEndpoint);
                    subscriber.Subscribe(topic);
                    while (true)
                    {
                        string topicRecv = subscriber.ReceiveFrameString();
                        // var bytes = subscriber.ReceiveFrameBytes();
                        var json = subscriber.ReceiveFrameString();
                        Console.WriteLine($"接收到：{json}");

                        //if (string.Compare(topicRecv, SiteConfiguration.PUBLISH_TOPIC_ORDER) == 0)
                        //{
                        //    var order = Utility.FromBytes<Order>(bytes);
                        //    Console.WriteLine($"接收到挂单：{order.Id}，价格：{order.Price}，交易量：{order.VolumeRemaining}");
                        //}
                        //else if (string.Compare(topicRecv, SiteConfiguration.PUBLISH_TOPIC_TICK) == 0)
                        //{
                        //    var tick = Utility.FromBytes<Tick>(bytes);
                        //    Console.WriteLine($"接收到最新价格：{tick.Price}，交易量：{tick.Volume}");
                        //}
                    }
                }
            });
        }
    }
}
